/* Copyright(c) 2010- Siro-info Co.(http://www.siro-info.com)
 * All rights reserved.
 */
package com.siro.pm.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.siro.pm.utils.DatabaseOperation;
import com.siro.pm.utils.DatabasePool;

/**
 * Class Declaration:
 * 
 * @version v1.0
 * @since Document.java v1.0
 */
public class Document implements Serializable {
	private static final long serialVersionUID = 909439711787462987L;
	private static final Logger logger = LoggerFactory
			.getLogger(Document.class);

	// id,文档名称，所属项目，存储路径
	private String id;
	private String name;
	private Project project;
	private String path;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Project getProject() {
		return project;
	}

	public void setProject(Project project) {
		this.project = project;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public static void insertDocument(Document doc) {
		logger.info("添加文档，id:{},name:{}", doc.getId(), doc.getName());
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO siropm_document VALUES('");
		sql.append(doc.getId()).append("','");
		sql.append(doc.getName()).append("','");
		sql.append(doc.getProject().getId()).append("','");
		sql.append(doc.getPath()).append("')");
		try {
			DatabaseOperation.excuteUpdateSQL(DatabasePool.getConnection(),
					sql.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("添加文档，id:{},name:{} 成功", doc.getId(), doc.getName());
	}

	public static void deleteDocument(Document document) {
		logger.info("删除文档，id:{},name:{}", document.getId(), document.getName());
		String sql = "DELETE FROM siropm_document WHERE id='"
				+ document.getId() + "'";
		try {
			DatabaseOperation
					.excuteUpdateSQL(DatabasePool.getConnection(), sql);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("删除文档，id:{},name:{} 成功", document.getId(),
				document.getName());
	}

	public static void updateDocument(Document document) {
		logger.info("更新文档，id:{} name:{}", document.getId(), document.getName());
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE siropm_document SET ");
		sql.append("name='").append(document.getName()).append("', ");
		sql.append("projectid='").append(document.getProject().getId())
				.append("', ");
		sql.append("path='").append(document.getPath()).append("'");
		sql.append("WHERE id='").append(document.getId()).append("'");
		try {
			DatabaseOperation.excuteUpdateSQL(DatabasePool.getConnection(),
					sql.toString());
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		logger.info("更新文档，id:{} name:{} 成功", document.getId(),
				document.getName());
	}

	public static List<Document> getAllDocuments() {
		logger.info("获取所有文档...");
		List<Document> documents = new ArrayList<Document>();
		String sql = "SELECT id,name,projectid,path FROM siropm_document";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				Document document = new Document();
				document.setId(row.get(0));
				document.setName(row.get(1));
				document.setProject(Project.getProject(row.get(2)));
				if (row.get(3) != null && !row.get(3).equals(""))
					document.setPath(row.get(3));
				documents.add(document);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return documents;
	}

	public static Document getDocument(String id) {
		logger.info("获取文档,ID:" + id);
		Document document = null;
		String sql = "SELECT id,name,projectid,path FROM siropm_document WHERE id='"
				+ id + "'";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			if (rslt == null || rslt.size() == 0)
				return null;
			List<String> row = rslt.get(1);
			document = new Document();
			document.setId(row.get(0));
			document.setName(row.get(1));
			document.setProject(Project.getProject(row.get(2)));
			if (row.get(3) != null && !row.get(3).equals(""))
				document.setPath(row.get(3));
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return document;
	}

	public static List<Document> getDocuments(Project project) {
		logger.info("获取与工程：{}相关的文档", project.getName());
		List<Document> documents = new ArrayList<Document>();
		String sql = "SELECT id,name,projectid,path FROM siropm_document WHERE projectid='"
				+ project.getId() + "'";
		try {
			Map<Integer, List<String>> rslt = DatabaseOperation
					.excuteSelectSQL(DatabasePool.getConnection(), sql);
			List<String> row = null;
			for (int i = 1; i <= rslt.size(); i++) {
				row = rslt.get(i);
				Document document = new Document();
				document.setId(row.get(0));
				document.setName(row.get(1));
				document.setProject(project);
				if (row.get(3) != null && !row.get(3).equals(""))
					document.setPath(row.get(3));
				documents.add(document);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		return documents;
	}

}
